# app/config/config.py
import json
import os
import torch
from flask import current_app
from transformers import BertTokenizer, BertConfig


class Config():
    def __init__(self):
        # 1. 基础路径配置（已有的正确路径逻辑保持不变）
        current_file_path = os.path.abspath(__file__)
        self.app_dir = os.path.dirname(os.path.dirname(current_file_path))
        self.data_path = os.path.join(self.app_dir, "data")
        self.save_model_path = os.path.join(self.app_dir, "save_models")

        # 确保目录存在
        os.makedirs(self.data_path, exist_ok=True)
        os.makedirs(self.save_model_path, exist_ok=True)

        # 2. 文件路径配置（已有的路径保持不变）
        self.tag2id_path = os.path.join(self.data_path, "tag2id.json")
        self.train_txt_path = os.path.join(self.data_path, "train.txt")
        self.label_path = os.path.join(self.data_path, "labels.json")
        self.vocab_txt_path = os.path.join(self.data_path, "vocab.txt")

        # 3. 新增：句子最大长度（解决'max_len'缺失问题）
        self.max_len = 128  # 根据你的数据调整，例如128、256，若不确定可先设为128

        # 4. 模型与训练参数（已有的保持不变）
        self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
        self.model_name = "BiLSTM_CRF"
        self.embedding_dim = 300
        self.hidden_dim = 256
        self.dropout = 0.2
        self.lr = 2e-3
        self.epochs = 10
        self.batch_size = 16

        #关系抽取相关配置
        #输入层
        self.bert_dim = 768
        #关系类别数
        self.num_rel = 18

        #配置路径
        self.bert_base_path = os.path.join(current_app.root_path, "bert_base_chinese")
        self.tokenizer = BertTokenizer.from_pretrained(self.bert_base_path)
        #bert配置文件
        self.bert_config = BertConfig.from_pretrained(self.bert_base_path)
        self.entertainment_data_path = os.path.join(current_app.root_path, "data","entertainment")
        #训练集数据路径
        self.bert_train_path = os.path.join(self.entertainment_data_path, "train.json")
        #验证机数据路径
        self.bert_dev_path = os.path.join(self.entertainment_data_path, "dev.json")
        #测试集数据路径
        self.bert_test_path = os.path.join(self.entertainment_data_path, "test.json")
        #标签分类路径
        self.bert_rel_path = os.path.join(self.entertainment_data_path, "relation.json")
        #关系类别相关的映射字典。类别数
        self.id2rel = json.load(open(self.bert_rel_path,encoding="utf-8"))
        self.rel2id = {v:int(k) for k,v in self.id2rel.items()}
        #模型训练相关的超参数
        self.bert_epoches = 10
        self.bert_batch_size = 16
        self.bert_learning_rate = 1e-5
        self.rel_size = len(self.rel2id)